<?php
// Include the header
include 'header.php';

// 引入数据库类
require_once '../class/Database.php';

// 创建数据库连接
$db = new Database();

// 获取查询参数
$name = isset($_GET['name']) ? $_GET['name'] : '';
$email = isset($_GET['email']) ? $_GET['email'] : '';
$repo_url = isset($_GET['repo_url']) ? $_GET['repo_url'] : '';

// 动态构建 SQL 查询
$sql = "SELECT * FROM dco_signatures WHERE 1=1"; // 1=1 用于简化后续拼接条件

// 根据用户输入的姓名、邮箱或仓库名过滤查询
$params = [];
if ($name) {
    $sql .= " AND name LIKE :name";
    $params[':name'] = "%$name%";
}
if ($email) {
    $sql .= " AND email LIKE :email";
    $params[':email'] = "%$email%";
}
if ($repo_url) {
    $sql .= " AND repo_url LIKE :repo_url";
    $params[':repo_url'] = "%$repo_url%";
}

// 按签署时间排序
$sql .= " ORDER BY signed_at DESC";

// 执行查询
$results = $db->query($sql, $params);

?>

<h2>已签署 DCO 的用户</h2>

<!-- 搜索表单 -->
<form method="get" action="status.php" class="mb-4">
    <div class="form-row">
        <div class="col-md-3">
            <label for="name">姓名</label>
            <input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8'); ?>" placeholder="姓名">
        </div>
        <div class="col-md-3">
            <label for="email">邮箱</label>
            <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); ?>" placeholder="邮箱">
        </div>
        <div class="col-md-3">
            <label for="repo_url">仓库名</label>
            <input type="text" class="form-control" id="repo_url" name="repo_url" value="<?php echo htmlspecialchars($repo_url, ENT_QUOTES, 'UTF-8'); ?>" placeholder="仓库名">
        </div>
        <div class="col-md-3 d-flex align-items-end">
            <button type="submit" class="btn btn-primary">搜索</button>
        </div>
    </div>
</form>

<?php if (empty($results)): ?>
    <p>当前没有符合条件的签署记录。</p>
<?php else: ?>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>姓名</th>
                <th>邮箱</th>
                <th>仓库名</th>
                <th>签署时间</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($results as $row): ?>
                <tr>
                    <td><?php echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8'); ?></td>
                    <td><?php echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8'); ?></td>
                    <td><?php echo htmlspecialchars($row['repo_url'], ENT_QUOTES, 'UTF-8'); ?></td>
                    <td><?php echo htmlspecialchars($row['signed_at'], ENT_QUOTES, 'UTF-8'); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

<?php
// Include the footer
include 'footer.php';
?>
